/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#ifndef _LYRICSPAINTER_H_
#define _LYRICSPAINTER_H_

#include <QObject>
#include <QPainter>
#include <QRect>
#include "factory.h"
#include "lyricsdata.h"

class LyricsPainter : public QObject {
    Q_OBJECT
public:
    enum DrawState {
        DrawMessage,
        DrawLyrics
    };
    LyricsPainter(QObject* parent = 0);
    virtual ~LyricsPainter();

    bool canPaintLyrics() const { return m_lyrics != NULL; };
    void setDrawState(DrawState state) { m_drawState = state; };
    void setLyrics(const LyricsData* lyrics) { m_lyrics = lyrics; setDrawState(DrawLyrics); };
    void clearLyrics() { m_lyrics = NULL; setDrawState(DrawMessage); };
    void setMessage(const QString& msg) { m_msg = msg; setDrawState(DrawMessage); };
    void setRect(const QRect& rect) { m_rect = rect; };
    virtual void draw(QPaintDevice* device, LyricsTime time);

protected:
    QFont m_font;
    QFontMetrics* m_fontMetrics;
    QRect m_rect;
    const LyricsData* m_lyrics;
    DrawState m_drawState;
    QString m_msg;
    QPaintDevice* m_device;

    void drawText(QPainter& painter, int x, int y, const QColor& color, const QString& text);

    virtual void drawLyrics(QPainter& painter, LyricsTime time) = 0;
    virtual void drawMessage(QPainter& painter, const QString& msg);

    void updateInterval(int pixels, int timeLength);

    /** Mix c1 and c2 with specific factor. */
    static QColor alphaBlending(const QColor& c1, const QColor& c2, const int factor);
    /** Determin which color shoule be used as highlighted color. */
    static QColor getFadingColor(const int timePass, const int timeLength);
    
signals:
    void intervalChanged(int);
};

typedef Factory<LyricsPainter> LyricsPainterManager;

#endif
